package com.ifast.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.domain.TreeNode;
import com.ifast.common.utils.Result;
import com.ifast.common.utils.ShiroUtils;
import com.ifast.sys.domain.DeptDO;
import com.ifast.sys.domain.UserGroupChildDO;
import com.ifast.sys.domain.UserGroupDO;
import com.ifast.sys.service.DeptService;
import com.ifast.sys.service.UserGroupChildService;
import com.ifast.sys.service.UserGroupService;
import com.ifast.sys.service.UserService;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * </pre>
 * 
 * <small> 2018年3月23日 | Aron</small>
 */
@RequestMapping("/sys/userGroup")
@Controller
public class UserGroupController extends AdminBaseController {

    private String prefix = "sys/userGroup";

    @Autowired
    UserGroupService userGroupService;

    @Autowired
    UserGroupChildService userGroupChildService;

    @Autowired
    DeptService deptService;

    @Autowired
    UserService userService;

    @RequiresPermissions("sys:usergroup:user")
    @Log("进入系统用户组列表页面")
    @GetMapping("")
    String userGroup(Model model) {
        return prefix + "/user_group";
    }

    
    @Log("查询系统用户组列表")
    @GetMapping("/list")
    @ResponseBody
    public Result<Page<UserGroupDO>> list(UserGroupDO userGroup) {
        // 查询列表数据
        if(StrUtil.isBlank(userGroup.getDeptId())){
            userGroup.setDeptId(ShiroUtils.getSysUser().getDeptId());
        }
        DeptDO deptDO = this.deptService.selectById(userGroup.getDeptId());
        userGroup.setDwCode(deptDO.getDwCode());
        Page<UserGroupDO> pageData = userGroupService.listPage(getPage(UserGroupDO.class),userGroup);
        return Result.ok(pageData);
    }


    @RequiresPermissions("sys:usergroup:add")
    @Log("添加用户组")
    @GetMapping("/add")
    String add(Model model) {
        return prefix + "/add";
    }


    @Log("保存用户组")
    @PostMapping("/save")
    @ResponseBody
    Result<String> save(UserGroupDO userGroup) {
        if(StrUtil.isBlank(userGroup.getUserIds())){
            return Result.fail();
        }
        if(StrUtil.isBlank(userGroup.getName())){
            return Result.fail();
        }
        userGroup.setDeptId(ShiroUtils.getSysUser().getDeptId());
        boolean flag = this.userGroupService.saveUserGroupDO(userGroup);
        if(!flag){
            return Result.fail();
        }
        return Result.ok();
    }

    @RequiresPermissions("sys:usergroup:edit")
    @Log("更新用户组")
    @GetMapping("/edit/{id}")
    String edit(Model model,@PathVariable("id") String uid) {
        UserGroupDO userGroupDO = this.userGroupService.selectById(uid);
        Wrapper<UserGroupChildDO> wrapper = new EntityWrapper<UserGroupChildDO>();
        wrapper.eq("user_group_Id",uid);
        List<UserGroupChildDO> userGroupChildDOList =  this.userGroupChildService.selectList(wrapper);
        String uidzs = userGroupChildDOList.stream().map(UserGroupChildDO::getUserId).collect(Collectors.joining(","));
        userGroupDO.setUserIds(uidzs);
        model.addAttribute("userGroup",userGroupDO);
        return prefix + "/edit";
    }

    @Log("查看用户组")
    @GetMapping("/view/{id}")
    String view(Model model,@PathVariable("id") String uid) {
        UserGroupDO userGroupDO = this.userGroupService.selectById(uid);
        Wrapper<UserGroupChildDO> wrapper = new EntityWrapper<UserGroupChildDO>();
        wrapper.eq("user_group_Id",uid);
        List<UserGroupChildDO> userGroupChildDOList =  this.userGroupChildService.selectList(wrapper);
        String uidzs = userGroupChildDOList.stream().map(UserGroupChildDO::getUserId).collect(Collectors.joining(","));
        userGroupDO.setUserIds(uidzs);
        model.addAttribute("userGroup",userGroupDO);
        return prefix + "/view";
    }

    @Log("更新用户组")
    @PostMapping("/update")
    @ResponseBody
    Result<String> update(UserGroupDO userGroup) {
        userGroup.setDeptId(ShiroUtils.getSysUser().getDeptId());
        boolean flag = userGroupService.updateUserGroupDO(userGroup);
        if(!flag){
            return Result.fail();
        }
        return Result.ok();
    }

    @Log("删除用户组")
    @PostMapping("/remove")
    @ResponseBody
    Result<String> remove(String id) {
        boolean flag = userGroupService.delUserGroupDO(id);
        if(!flag){
            return Result.fail();
        }
        return Result.ok();
    }


    /**
     * 初始化使用
     * @return
     */
    @PostMapping("/zTree")
    @ResponseBody
    @Log("查询部门树形数据")
    public List<TreeNode> zTree(){
        return deptService.getDeptByAuth(ShiroUtils.getSysUser().getDeptId());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @PostMapping("/zTreeByPid")
    @ResponseBody
    @Log("查询部门树形数据")
    public List<TreeNode> zTreesByPid(String id){
        return deptService.getZTree(id);
    }


    @GetMapping("/treeView")
    @Log("进入部门树形显示页面")
    String treeView( Model model) {
        model.addAttribute("deptId", ShiroUtils.getSysUser().getDeptId());
        return prefix + "/dept_and_user";
    }

    @Log("查询系统用户列表")
    @GetMapping("/listUserPage")
    public String listUserPage(Model model,String deptId) {
        model.addAttribute("deptId", deptId);
        return prefix + "/user_select";
    }

}
